Kotlin

Inline Functions

Swift
                  lock(l) { foo() }
                
                    lock(l) { foo() }
                  
                  l.lock()
try {
    foo()
}
finally {
    l.unlock()
}
                
                    l.lock()
try {
    foo()
}
finally {
    l.unlock()
}
                  
                  inline fun <T> lock(lock: Lock, body: () -> T): T { ... }
                
                    inline func <T> lock(lock: Lock, body: () -> T): T { ... }
                  

noinline

                  inline fun foo(inlined: () -> Unit, noinline notInlined: () -> Unit) { ... }
                
                    inline func foo(inlined: () -> Unit, noinline notInlined: () -> Unit) { ... }
                  

Non-local returns

                  fun foo() {
    ordinaryFunction {
        return // ERROR: cannot make `foo` return here
    }
}
                
                    func foo() {
    ordinaryFunction {
        return // ERROR: cannot make `foo` return here
    }
}
                  
                  fun foo() {
    inlined {
        return // OK: the lambda is inlined
    }
}
                
                    func foo() {
    inlined {
        return // OK: the lambda is inlined
    }
}
                  
                  fun hasZeros(ints: List<Int>): Boolean {
    ints.forEach {
        if (it == 0) return true // returns from hasZeros
    }
    return false
}
                
                    func hasZeros(ints: List<Int>): Boolean {
    ints.forEach {
        if ($0 == 0) return true // returns from hasZeros
    }
    return false
}
                  
                  inline fun f(crossinline body: () -> Unit) {
    val f = object: Runnable {
        override fun run() = body()
    }
    // ...
}
                
                    inline func f(crossinline body: () -> Unit) {
    let f = object: Runnable {
        override func run() = body()
    }
    // ...
}
                  

Reified type parameters

                  fun <T> TreeNode.findParentOfType(clazz: Class<T>): T? {
    var p = parent
    while (p != null && !clazz.isInstance(p)) {
        p = p.parent
    }
    @Suppress("UNCHECKED_CAST")
    return p as T?
}
                
                    func <T> TreeNode.findParentOfType(clazz: Class<T>): T? {
    var p = parent
    while (p != null && !clazz.isInstance(p)) {
        p = p.parent
    }
    @Suppress("UNCHECKED_CAST")
    return p as T?
}
                  
                  treeNode.findParentOfType(MyTreeNode::class.java)
                
                    treeNode.findParentOfType(MyTreeNode::class.java)
                  
                  treeNode.findParentOfType<MyTreeNode>()
                
                    treeNode.findParentOfType<MyTreeNode>()
                  
                  inline fun <reified T> TreeNode.findParentOfType(): T? {
    var p = parent
    while (p != null && p !is T) {
        p = p.parent
    }
    return p as T?
}
                
                    inline func <reified T> TreeNode.findParentOfType(): T? {
    var p = parent
    while (p != null && p !is T) {
        p = p.parent
    }
    return p as T?
}
                  
                  inline fun <reified T> membersOf() = T::class.members
​
fun main(s: Array<String>) {
    println(membersOf<StringBuilder>().joinToString("\n"))
}
                
                    inline func <reified T> membersOf() = T::class.members
​
func main(s: Array<String>) {
    print(membersOf<StringBuilder>().joinToString("\n"))
}
                  

Inline properties (since 1.1)

                  val foo: Foo
    inline get() = Foo()
​
var bar: Bar
    get() = ...
    inline set(v) { ... }
                
                    let foo: Foo
    inline get() = Foo()
​
var bar: Bar
    get() = ...
    inline set(v) { ... }
                  
                  inline var bar: Bar
    get() = ...
    set(v) { ... }
                
                    inline var bar: Bar
    get() = ...
    set(v) { ... }